<?php
/**
 * 默认控制器，方法里面代码可以删除
 */
class IndexControl extends CommonControl{
	private $db;
		
	public function index(){
		//分配top数据
		$this->assign_data();
		// M('stu')->all();
		// M('stu')->query('SELECT * FROM stu');
		$sql='SELECT * FROM hd_category WHERE pid=0';
		$data=$this->db->query($sql);
		foreach ($data as $k => $v) {
			$sql1="SELECT * FROM hd_category WHERE pid=$v[cid]";
			$data[$k]['son']=$this->db->query($sql1);
		}
		//未解决问题
		$field='asid,content,answer,cid';
		$nosolve=M('ask')->where('solve=0')->order('time DESC')->field($field)->limit(4)->all();
		$this->assign('nosolve',$nosolve);
		//高分悬赏问题
		$filed1="asid,content,answer,reward,cid";
		$upgold=M('ask')->where('solve=0 and reward>20')->order('reward DESC')->field($filed1)->limit(4)->all();
		$this->assign('upgold',$upgold);

		
		// 左侧分类二级菜单加入模板
		$this->assign('data',$data);

		$this->display();
	}
	public function __construct(){
		parent::__construct();
		$this->db=M('category');
	}
	public function search(){
		if(isset($_POST['search'])){
			$search=$_POST['search'];
			// var_dump($search);die;
			$field='ask.asid,ask.content as askcontent,an.content as ancontent,title,ask.time,answer ';
			$data1=M('category')->query("SELECT {$field} FROM hd_answer as an join hd_ask as ask on an.asid=ask.asid join hd_category as cate on ask.cid=cate.cid where accept=1 and solve=1 and ask.content like '%{$search}%' limit 10 ");
			// var_dump($data1);die;
			if(!is_null($data1))$this->assign('search',$data1);
		}
		$this->display();
	}
	
	
}